<?php
class M_Episode extends CI_Model{
    var $_episode_id;
    var $_movie_id;
    var $_name;
    var $_path;
    private $_table = 'movie_episode';
    function __construct() {
        parent::__construct();
    }

    /**
     * @param  $episodeID
     * @return string array
     */
    function getEpisodePathList($episodeID){
        $this->db->cache_on();
        //return an array of playlist path
        $sql = 'select path
                from movie_episode
                where episode_id = ' . $episodeID;
        
        $query = $this->db->query($sql);
        $result = $query->row_array();
        return $result['path'];
    }    
    
    /**
     *laty thong tin cua episode dang xem
     * @param type $episodeID
     * @return type array gom movie_name, episode_name, movie_image
     */
    function getEpisodeInfo($episodeID){
        $this->db->cache_on();
        $sql = "SELECT ep.name AS episode_name, ep.views AS episode_views, m.name AS movie_name, m.image AS movie_image, m.rating AS movie_rating, m.movie_id AS movie_id, ep.path AS episode_path
                FROM movie_episode ep, movies m
                WHERE ep.movie_id = m.movie_id
                AND ep.episode_id = ?";
        
        $query = $this->db->query($sql, array($episodeID));
        
        return $query->row_array();
    }


    
    /**
     *danh sach id cac tap film cua tep film dang xem
     * @param type $episodeID 
     * @return episode ID
     */
    function getEpisodeIDList($episodeID){
        $this->db->cache_on();
        $sql = "SELECT ep.episode_id, ep.name
                FROM movie_episode ep
                WHERE ep.movie_id = (
                                    SELECT e.movie_id
                                    FROM movie_episode e
                                    WHERE e.episode_id = ? )";
        
        $query = $this->db->query($sql, array($episodeID));
        return $query->result_array();
    }


    /**lay episode dau tien
     * @param  $movieID
     * @return episode array
     */
    function getFirstEpisode($movieID){
        $sql = '';
        return;
    }

    /**
     * @param  $episodeID
     * @return int
     */
    function getMovieID($episodeID){
        $this->db->cache_on();
        $sql = "SELECT movie_id
                FROM movie_episode
                WHERE episode_id = $episodeID";
        $query = $this->db->query($sql);
        $result = $query->row_array();
        return $result['movie_id'];
    }

    /**
     * @return int
     */
    function addNew() {
        $episode =array(
            'name' => $this->_name,
            'movie_id' => $this->_movie_id,
            'path' => $this->_path
        );
        $this->db->insert($this->_table, $episode);
        return $this->db->insert_id();
    }

    /**
     * tang so luot xem len 1
     * @param  $episodeID
     * @param  $curViews so luot xem hien tai
     * @return void
     */
    function addView($episodeID, $curViews){
        $curViews += 1;
        $sql = "UPDATE movie_episode SET views = $curViews WHERE episode_id = $episodeID";
        $this->db->query($sql);
    }

    function update(){

        $sql = "update movie_episode
                SET name = '$this->_name', path = '$this->_path'
                WHERE episode_id = $this->_episode_id";
        
        $this->db->query($sql);
    }

}
?>
